home *** CD-ROM | disk | FTP | other *** search
/ MACup: Giveaway 1996 / Image.iso / Shareware & Demos / Web-Publishing / SNAP PrimeBase / Telephone & Tabler / Click Me! < prev    next >
Encoding:
Text File  |  1996-06-19  |  14.0 KB  |  675 lines  |  [TEXT/Itb1]

  1. #Telephone Application
  2. #=====================
  3.  
  4. #Login
  5. #-----
  6.  
  7. /* THE NAME OF THE DATABASE TO USE: */
  8. varchar DatabaseName        = "Telephone";
  9.  
  10. integer $DefCols            = 1;    
  11. integer $ClearRows            = 2;    
  12. integer $SetTitle            = 3;    
  13. integer $EnableBut            = 4;    
  14. integer $FillPopup            = 5;    
  15. integer $FillList            = 6;    
  16. integer $SetText            = 7;    
  17. integer $AddRow                = 8;    
  18. integer $DelRow                = 9;    
  19. integer $SetCol                = 10;
  20. integer $SetWindow            = 11;
  21. integer $PickRow            = 12;
  22. integer $PickCol            = 13;    
  23. integer $Alert                = 14;    
  24. integer $Progress            = 15;    
  25. integer $Debug                = 16;    
  26. integer $Stop                = 17;    
  27. integer $Halt                = 18;    
  28.  
  29. /* Alert types: */
  30. integer $stop                = 1;
  31. integer $caution            = 2;    
  32. integer $note                = 3;    
  33.  
  34. varchar LastTitle            = "";
  35. integer LastTitleCol        = 0;
  36. integer AlertSignal            = 0;
  37. integer $DoDeleteDept        = 1;
  38. integer $DoDeleteType        = 2;
  39. integer $DoDeleteNumber        = 3;
  40. integer $DoCreateDB            = 4;
  41.  
  42. integer NewID                = 0;
  43. /**
  44. print $debug, "Progress test begins...";
  45. int i = 1;
  46. for (;;) {
  47.     i++;
  48.     $yield();
  49.     print $progress, i/10, "Counting to 1000...", 1;
  50.     if (i > 1000) break;
  51. }
  52. print $debug, "Test ends.";
  53. **/
  54.  
  55. boolean found = $false;
  56.  
  57. describe databases;
  58. for each {
  59.     if (->1 == DatabaseName) {
  60.         found = $true;
  61.         break;
  62.     }
  63. }
  64.  
  65. if (found)
  66.     open database :DatabaseName;
  67. else {
  68.     print $alert, $stop, DatabaseName + " database not found! Should I create it?", 2, "Create", "Cancel";
  69.     AlertSignal = $DoCreateDB;
  70. }
  71.  
  72. #ALERT
  73. if (AlertSignal == $DoCreateDB) {
  74.     if (@how == 1)
  75.         execute file "CreateDB";
  76.     else
  77.         /* If cancel, then quit! */
  78.         print $halt;
  79. }
  80.  
  81. #ERROR
  82. /* If an error occurs on login, then we halt the program! */
  83. print 18; /* ($halt) - If an error occurs, it might not be defined! */
  84.  
  85. #Window 'WT' "Number Types"
  86. #--------------------------
  87.  
  88. print $DefCols, 2, "Code", "Name";
  89. select * from Type
  90. into cType for extract;
  91. print $ClearRows;
  92. for each cType {
  93.     print $AddRow;
  94.     printrow cType;
  95. }
  96. print $PickCol, 2;
  97. print $EnableBut, 1, 1, 0, 0;
  98.  
  99. #SELECT
  100.  
  101. select * from Type
  102. into cType for extract;
  103. print $ClearRows;
  104. for each cType {
  105.     print $AddRow;
  106.     printrow cType;
  107. }
  108. print $EnableBut, 1, 1, 0, 0;
  109.  
  110. #UPDATE
  111.  
  112. if (@col == 2 and @row != 0) {
  113.     print $SetCol, 2, @text;
  114.     $updaterow(cType, 2, @text);
  115.     update Type set name = @text where code = cType->1;
  116. }
  117.  
  118. #PICK
  119.  
  120. if (@row == 0) {
  121.     print $EnableBut, 1, 1, 0, 0;
  122.     print $SetText, "";
  123. }
  124. else {
  125.     fetch absolute @row of cType;
  126.     if (@col == 1) {
  127.         print $EnableBut, 1, 1, 0, 1;
  128.         print $SetText, "";
  129.     }
  130.     else {
  131.         print $EnableBut, 1, 1, 1, 1;
  132.         print $SetText, cType->@col;
  133.     }
  134. }
  135.  
  136. #INSERT
  137.  
  138. begin;
  139. /* Get the next id: */
  140. select max(code)+1 from Type;
  141. fetch;
  142. if ($sqlcode == $sqlnotfound)
  143.     NewID = 1;
  144. else
  145.     NewID = ->1;
  146. /* Insert into the database: */
  147. insert Type values (NewID, "NewType" + varchar(NewID));
  148. commit;
  149.  
  150. /* Update memory cursor */
  151. fetch last of cType;
  152. fetch next of cType;
  153. $insertrow(cType,  NewID, "NewType" + varchar(NewID));
  154.  
  155. /* Print to screen */
  156. print $AddRow, NewID, "NewType" + varchar(NewID);
  157.  
  158. /* Select the new row: */
  159. fetch last of cType;
  160. print $PickRow, $rows(cType);
  161. if (@col == 1) {
  162.     print $EnableBut, 1, 1, 0, 1;
  163.     print $SetText, "";
  164. }
  165. else {
  166.     print $EnableBut, 1, 1, 1, 1;
  167.     print $SetText, cType->@col;
  168. }
  169.  
  170. #DELETE
  171.  
  172. if (@row > 0) {
  173.     select count(*) from number where type = cType->1;
  174.     fetch;
  175.     if (->1 == 0) {
  176.         AlertSignal = $DoDeleteType;
  177.         print $alert, $stop, "Do you really want to delete the number type '"+cType->2+"'?", 2, "Delete", "Cancel";        
  178.     }
  179.     else
  180.         print $alert, $stop, "The number type '"+cType->2+"' cannot be deleted, it in use!", 1, "OK";
  181. }
  182.  
  183. #ALERT
  184.  
  185. if (AlertSignal == $DoDeleteType) {
  186.      AlertSignal = 0;
  187.     if (@row > 0 and @how == 1) {
  188.         delete Type where code = cType->1;
  189.  
  190.         fetch absolute @row of cType;
  191.         $deleterow(cType);
  192.  
  193.         print $DelRow;
  194.  
  195.         if (@row > $rows(cType)) {
  196.             print $EnableBut, 1, 1, 0, 0;
  197.             print $SetText, "";
  198.         }
  199.         else {
  200.             if (@col == 1) {
  201.                 print $EnableBut, 1, 1, 0, 0;
  202.                 print $SetText, "";
  203.             }
  204.             else {
  205.                 print $EnableBut, 1, 1, 1, 1;
  206.                 print $SetText, cType->@col;
  207.             }
  208.         }
  209.     }
  210. }
  211.  
  212. #Window 'WD' "Departments"
  213. #-------------------------
  214.  
  215. print $DefCols, 2, "Code", "Name";
  216. select * from Department
  217. into cDept for extract;
  218. print $ClearRows;
  219. for each cDept {
  220.     print $AddRow;
  221.     printrow cDept;
  222. }
  223. print $PickCol, 2;
  224. print $EnableBut, 1, 1, 0, 0;
  225.  
  226. #SELECT
  227.  
  228. select * from Department
  229. into cDept for extract;
  230. print $ClearRows;
  231. for each cDept {
  232.     print $AddRow;
  233.     printrow cDept;
  234. }
  235. print $EnableBut, 1, 1, 0, 0;
  236.  
  237. #UPDATE
  238.  
  239. if (@col == 2 and @row != 0) {
  240.     print $SetCol, 2, @text;
  241.     $updaterow(cDept, 2, @text);
  242.     update Department set name = @text where code = cDept->1;
  243. }
  244.  
  245. #PICK
  246.  
  247. if (@row == 0) {
  248.     print $EnableBut, 1, 1, 0, 0;
  249.     print $SetText, "";
  250. }
  251. else {
  252.     fetch absolute @row of cDept;
  253.     if (@col == 1) {
  254.         print $EnableBut, 1, 1, 0, 1;
  255.         print $SetText, "";
  256.     }
  257.     else {
  258.         print $EnableBut, 1, 1, 1, 1;
  259.         print $SetText, cDept->@col;
  260.     }
  261. }
  262.  
  263. #INSERT
  264.  
  265. begin;
  266. /* Get the next id: */
  267. select max(code)+1 from Department;
  268. fetch;
  269. if ($sqlcode == $sqlnotfound)
  270.     NewID = 1;
  271. else
  272.     NewID = ->1;
  273. /* Insert into the database: */
  274. insert Department values (NewID, "NewDept" + varchar(NewID));
  275. commit;
  276.  
  277. /* Update memory cursor */
  278. fetch last of cDept;
  279. fetch next of cDept;
  280. $insertrow(cDept, NewID, "NewDept" + varchar(NewID));
  281.  
  282. /* Print to screen */
  283. print $AddRow, NewID, "NewDept" + varchar(NewID);
  284.  
  285. /* Select the new row: */
  286. fetch last of cDept;
  287. print $PickRow, $rows(cDept);
  288. if (@col == 1) {
  289.     print $EnableBut, 1, 1, 0, 1;
  290.     print $SetText, "";
  291. }
  292. else {
  293.     print $EnableBut, 1, 1, 1, 1;
  294.     print $SetText, cDept->@col;
  295. }
  296.  
  297. #DELETE
  298.  
  299. if (@row > 0) {
  300.     select id from person where department = cDept->1;
  301.     fetch;
  302.     if ($sqlcode == $sqlnotfound) {
  303.         AlertSignal = $DoDeleteDept;
  304.         print $alert, $stop, "Do you really want to delete '"+cDept->2+"'?", 2, "Delete", "Cancel";        
  305.     }
  306.     else
  307.         print $alert, $stop, "The department '"+cDept->2+"' cannot be deleted, it in use!", 1, "OK";
  308. }
  309.  
  310. #ALERT
  311.  
  312. if (AlertSignal == $DoDeleteDept) {
  313.     AlertSignal = 0;
  314.     if (@row > 0 and @how == 1) {
  315.         delete Department where code = cDept->1;
  316.  
  317.         fetch absolute @row of cDept;
  318.         $deleterow(cDept);
  319.  
  320.         print $DelRow;
  321.  
  322.         if (@row > $rows(cDept)) {
  323.             print $EnableBut, 1, 1, 0, 0;
  324.             print $SetText, "";
  325.         }
  326.         else {
  327.             if (@col == 1) {
  328.                 print $EnableBut, 1, 1, 0, 0;
  329.                 print $SetText, "";
  330.             }
  331.             else {
  332.                 print $EnableBut, 1, 1, 1, 1;
  333.                 print $SetText, cDept->@col;
  334.             }
  335.         }
  336.     }
  337. }
  338.  
  339. #Window 'WN' "Telephone Numbers"
  340. #-----------------------------
  341.  
  342. print $EnableBut, 1, 1, 0, 0;
  343. print $DefCols, 5, "Type", "Name", "Surname", "Department", "Number";
  344. print $FillPopup, 3, "<SELECT ALL>", "<SELECT ALL>", "Is Equal To:", "Contains:";
  345. select Name from Type for extract;
  346. print $FillList, $rowcnt; 
  347. for each print ->1;
  348.  
  349. select Type.Name, Person.Name, Person.Surname, Department.Name, Number.Number, Number.Person as kperson, Number.Type as ktype
  350. from Department, Type, Person, Number
  351. where $false
  352. into cTelNum for extract;
  353.  
  354. #SELECT
  355.  
  356. if (LastTitleCol > 0) {
  357.     print $SetTitle, LastTitleCol, LastTitle;
  358.     LastTitleCol = 0;
  359. }
  360.  
  361. if (@popup == "<SELECT ALL>") {
  362.     select Type.Name, Person.Name, Person.Surname, Department.Name, Number.Number, Number.Person as kperson, Number.Type as ktype
  363.     from Department, Type, Person, Number
  364.     where
  365.     Type.Code == Number.Type and
  366.     Number.Person = Person.ID and
  367.     Person.Department == Department.Code
  368.     into cTelNum for extract;
  369. }
  370. else {
  371.     varchar string, title;
  372.  
  373.     string =
  374.     "select Type.Name, Person.Name, Person.Surname, Department.Name, Number.Number, Number.Person as kperson, Number.Type as ktype " +
  375.     "from Department, Type, Person, Number where " +
  376.     "Type.Code == Number.Type and " +
  377.     "Number.Person = Person.ID and " +
  378.     "Person.Department == Department.Code and ";
  379.  
  380.     /* COLUMN */
  381.     switch (@col) {
  382.         case 1:
  383.             string = string + "Type.Name ";
  384.             title = "Type";
  385.             break;
  386.         case 2:
  387.             string = string + "Person.Name ";
  388.             title = "Name";
  389.             break;
  390.         case 3:
  391.             string = string + "Person.Surname ";
  392.             title = "Surname";
  393.             break;
  394.         case 4:
  395.             string = string + "Department.Name ";
  396.             title = "Department";
  397.             break;
  398.         case 5:
  399.             string = string + "varchar Number.Number ";
  400.             title = "Number";
  401.             break;
  402.     }
  403.     LastTitle = title;
  404.     LastTitleCol = @col;
  405.  
  406.     /* CONDITION */
  407.     if (@popup == "Is Equal To:") {
  408.         string = string + "= '";
  409.         title = title + " = '";
  410.     }
  411.     else {
  412.         string = string + "LIKE '*";
  413.         title = title + " LIKE '";
  414.     }
  415.     
  416.     /* VALUE */
  417.     string = string + @text;
  418.     title = title + @text +"'";
  419.  
  420.     /* CONDITION */
  421.     if (@popup == "Is Equal To:")
  422.         string = string + "' ";
  423.     else
  424.         string = string + "*' ";
  425.  
  426.     string = string + 
  427.     "into cTelNum for extract;";
  428.  
  429.     print $SetTitle, @col, title;
  430.     execute string;
  431. }
  432.  
  433. print $ClearRows;
  434. for each cTelNum {
  435.     print $AddRow;
  436.     print cTelNum->1, cTelNum->2, cTelNum->3, cTelNum->4, cTelNum->5;
  437. }
  438.  
  439. #PICK
  440.  
  441. if (@col == 1) {
  442.     select Name from Type for extract;
  443.     print $FillList, $rowcnt; 
  444.     for each print ->1;
  445. }
  446. else if (@col == 4) {
  447.     select Name from Department for extract;
  448.     print $FillList, $rowcnt; 
  449.     for each print ->1;
  450. }
  451. else
  452.     print $FillList, 0;
  453.  
  454. if (@row == 0)
  455.     print $EnableBut, 1, 1, 0, 0;
  456. else {
  457.     print $EnableBut, 1, 1, 1, 1;
  458.     fetch absolute @row of cTelNum;
  459.     print $SetText, cTelNum->@col;
  460. }
  461.  
  462. #UPDATE
  463.  
  464. if (@row != 0) {
  465.     fetch absolute @row of cTelNum;
  466.     switch (@col) {
  467.         case 1:
  468.             select * from Type where name = @text;
  469.             fetch;
  470.             if ($sqlcode != $sqlnotfound and cTelNum->1 !=  ->2) {
  471.                 select * from number where key = { cTelNum->kperson, ->1 } into cTmp for extract;
  472.                 if ($rows(cTmp) > 0) {
  473.                     print $alert, $stop, cTelNum->2 + " " + cTelNum->3 + " already has a number of type '" + cTelNum->1 + "'.", 1, "OK";
  474.                 }
  475.                 else {
  476.                     print $SetCol, 1, ->2;
  477.                     $updaterow(cTelNum, 1, ->2);
  478.                     update Number set type = ->1 where key = { cTelNum->kperson, cTelNum->ktype };
  479.                 }
  480.             }
  481.             break;
  482.         case 2:
  483.             print $SetCol, 2, @text;
  484.             $updaterow(cTelNum, 2, @text);
  485.             update Person set Name = @text where ID = cTelNum->kperson;
  486.             break;
  487.         case 3:
  488.             print $SetCol, 3, @text;
  489.             $updaterow(cTelNum, 3, @text);
  490.             update Person set SurName = @text where ID = cTelNum->kperson;
  491.             break;
  492.         case 4:
  493.             /* Update the persons department */
  494.             select * from Department where name = @text;
  495.             fetch;
  496.             if ($sqlcode != $sqlnotfound) {
  497.                 print $SetCol, 4, ->2;
  498.                 $updaterow(cTelNum, 4, ->2);
  499.                 update Person set Department = ->1 where ID = cTelNum->kperson;
  500.             }
  501.             break;
  502.         case 5:
  503.             print $SetCol, 5, @text;
  504.             $updaterow(cTelNum, 5, @text);
  505.             update Number set Number = @text where key = { cTelNum->kperson, cTelNum->ktype };
  506.             break;            
  507.     }
  508. }
  509.  
  510. #INSERT
  511. /* Add a new number... */
  512. varchar newtype = "";
  513. varchar newname = "";
  514. varchar newsurname = "";
  515. varchar newdept = "";
  516. varchar newnumber = "";
  517. boolean doinsert = $true;
  518.  
  519. switch (@col) {
  520.     case 1: newtype = @text; break;
  521.     case 2: newname = @text; break;
  522.     case 3: newsurname = @text; break;
  523.     case 4: newdept = @text; break;
  524.     case 5: newnumber = @text; break;
  525. }
  526.  
  527. if (@row == 0) {
  528.     /* New department for the person: */
  529.     if (newdept != "") {
  530.         select * from Department where name = newdept;
  531.         fetch;
  532.         if ($sqlcode == $sqlnotfound)
  533.             newdept = "";
  534.     }
  535.     
  536.     if (newdept == "") {
  537.         select * from Department;
  538.         fetch;
  539.     }
  540.  
  541.     /* Add a new person first... */
  542.     /* Get the next id: */
  543.     begin;
  544.     select max(ID)+1 from Person into cTmp;
  545.     fetch of cTmp;
  546.     if ($sqlcode == $sqlnotfound)
  547.         NewID = 100;
  548.     else
  549.         NewID = cTmp->1;
  550.  
  551.     /* Insert into the database: */
  552.     if (newsurname == "")
  553.         newsurname = "Person" + varchar(NewID);
  554.     insert Person values (NewID, newname, newsurname, ->1);
  555.     commit;
  556.  
  557.     if (newtype != "") {
  558.         select * from Type where name = newtype into cTmp2;
  559.         fetch next of cTmp2;
  560.         if ($sqlcode == $sqlnotfound)
  561.             newtype = "";
  562.     }
  563.  
  564.     if (newtype == "") {
  565.         select * from Type into cTmp2;
  566.         fetch next of cTmp2;
  567.     }
  568.  
  569.     fetch last of cTelNum;
  570.     fetch next of cTelNum;
  571.     $insertrow(cTelNum, cTmp2->2, newname, newsurname, ->2, newnumber, NewID, cTmp2->1);
  572. }
  573. else {
  574.     /* New number for existing person... */
  575.     fetch absolute @row of cTelNum;
  576.     if (newtype != "") {
  577.         select * from Type where name = newtype;
  578.         fetch;
  579.         if ($sqlcode == $sqlnotfound)
  580.             newtype = "";
  581.     }
  582.  
  583.     if (newtype != "") {
  584.         /* Number of given type: */
  585.         select * from number where key = { cTelNum->kperson, ->1 } into cTmp for extract;
  586.         fetch of cTmp;
  587.         if ($sqlcode != $sqlnotfound) {
  588.             print $alert, $stop, cTelNum->2 + " " + cTelNum->3 + " already has a number of type '" + cTelNum->1 + "'.", 1, "OK";
  589.             doinsert = $false;
  590.         }
  591.     }
  592.     else {
  593.         /* Number of ANY type: */
  594.         select * from type where code not in (select type from number where person == cTelNum->kperson);
  595.         fetch;
  596.         if ($sqlcode == $sqlnotfound) {
  597.             print $alert, $stop, cTelNum->2 + " " + cTelNum->3 + " already has all types of numbers.", 1, "OK";
  598.             doinsert = $false;
  599.         }
  600.     }
  601.  
  602.     if (doinsert) {
  603.         integer newperson;
  604.  
  605.         newname = cTelNum->2;
  606.         newsurname = cTelNum->3;
  607.         newdept = cTelNum->4;
  608.         newperson = cTelNum->kperson;
  609.         fetch last of cTelNum;
  610.         fetch next of cTelNum;
  611.         $insertrow(cTelNum, ->2, newname, newsurname, newdept, newnumber, newperson, ->1);
  612.     }
  613. }
  614.  
  615. if (doinsert) {
  616.     /* Insert the number... */
  617.     fetch last of cTelNum;
  618.     insert Number values (cTelNum->kperson, cTelNum->ktype, newnumber);
  619.  
  620.     /* Print to screen */
  621.     print $AddRow, cTelNum->1, cTelNum->2, cTelNum->3, cTelNum->4, cTelNum->5;
  622.  
  623.     print $EnableBut, 1, 1, 1, 1;
  624.     print $PickRow, $rows(cTelNum);
  625.     print $SetText, cTelNum->@col;
  626. }
  627.  
  628. #DELETE
  629.  
  630. procedure DeleteNumber()
  631. {
  632.     if (@row != 0) {
  633.         fetch absolute @row of cTelNum;
  634.         select count(*) from number where person = cTelNum->kperson;
  635.         fetch;
  636.         if (->1 == 1)
  637.             delete Person where id = cTelNum->kperson;
  638.  
  639.         delete Number where key = { cTelNum->kperson, cTelNum->ktype };
  640.         $deleterow(cTelNum);
  641.         print $DelRow;
  642.  
  643.         if (@row > $rows(cTelNum)) {
  644.             print $EnableBut, 1, 1, 0, 0;
  645.             print $SetText, "";
  646.         }
  647.         else {
  648.             print $EnableBut, 1, 1, 1, 1;
  649.             print $SetText, cTelNum->@col;
  650.         }
  651.     }
  652. }
  653. end procedure DeleteNumber;
  654.  
  655. /* Delete a telephone number: */
  656. if (@row != 0) {
  657.     fetch absolute @row of cTelNum;
  658.     select count(*) from number where person = cTelNum->kperson;
  659.     fetch;
  660.     if (->1 <= 1) {
  661.         AlertSignal = $DoDeleteNumber;
  662.         print $alert, $stop, "Deleting this number will delete the person: " + cTelNum->2 + " " + cTelNum->3 + ". Delete anyway?", 2, "Delete", "Cancel";        
  663.     }
  664.     else
  665.         DeleteNumber();
  666. }
  667.  
  668. #ALERT
  669.  
  670. if (AlertSignal == $DoDeleteNumber and @how == 1) {
  671.      AlertSignal = 0;
  672.      DeleteNumber();
  673. }
  674.  
  675.